package com.bdq.user.infrastructure.config;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.bdq.common.constants.Constant;
import com.bdq.user.infrastructure.config.properties.AliYunProperties;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author malei
 * @date 2024-05-03 14:19
 */
@Configuration
public class AliYunConfig {

    @Resource
    private AliYunProperties aliYunProperties;

    @Bean
    public AsyncClient aliYunAsyncClient(){
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(aliYunProperties.getAccessKeyId())
                .accessKeySecret(aliYunProperties.getAccessKeySecret())
                .build());
        return AsyncClient.builder()
                .region(Constant.ALI_YUN_REGION_ID)
                .credentialsProvider(provider)
                .overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride(Constant.ALI_YUN_ENDPOINT_OVERRIDE))
                .build();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()

                .accessKeyId("LTAI5t6TonbYJNts5RQdLy88")
                .accessKeySecret("2nnVx78GNlSNerOygyR5hkV1LyL8Iw")

                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou")

                .credentialsProvider(provider)

                .overrideConfiguration(
                        ClientOverrideConfiguration.create()

                                .setEndpointOverride("dysmsapi.aliyuncs.com")

                )
                .build();

        // Parameter settings for API request
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("阿里云短信测试")
                .templateCode("SMS_154950909")
                .phoneNumbers("13012590583")
                .templateParam("{\"code\":\"0324\"}")

                .build();


        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);

        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }
}
